<?php namespace Admin\Controller;
class StuController extends CommonController{
    public function index(){
        if (IS_POST){
            //获取post提交的班级名称
            p($_POST);
            $sname = $_POST['sname'];
            $age = $_POST['age'];
            $sex = $_POST['sex'];
            $cid = $_POST['cid'];

            //调用model里面的没有结果集的exec方法
            M()->exec("insert into stu (sname,age,sex,cid) values ('$sname','$age','$sex','$cid')");
            //调用controller里面的成功提示的方法
            $this->success("添加成功","Index.php?m=Admin&c=stu&cid=$cid"); exit;
        }
        $this->assign("title","学生管理");
        $cid=$_GET['cid'];
        $data=M()->query("select * from stu  where cid=$cid");
        //p($data);
        $this->assign("data",$data);
        $this->display();
    }
    //编辑
    public function edit()
    {
        if(IS_POST)
        {
            //1.接受post提交参数
            $sid = $_POST['sid'];
            $sname = $_POST['sname'];
            $age = $_POST['age'];
            $sex = $_POST['sex'];
            //2.执行修改
            M()->exec("update stu set sname='{$sname}',age={$age},sex='$sex' where sid={$sid}");
            //3.成功提示
            $this->success("修改成功",$_SERVER['HTTP_REFERER']);exit;
        }
        $this->error("非法请求","Index.php?m=Admin&c=stu");
    }
    //删除
    public function del()
    {
        if(IS_POST)
        {
            $sid = $_POST['sid'];
//            p($_SERVER);die; 获取一系列的信息
            M()->exec("delete from stu where sid={$sid}");
            //3.成功提示 $_SERVER['HTTP_REFERER']返回历史上一页
            $this->success("删除成功",$_SERVER['HTTP_REFERER']);exit;
        }
        $this->error("非法请求","Index.php?m=Admin&c=stu");
    }
}
